<?php

class Router
{

        public $routes = array();
        // 数据格式, [url->[ method->[controller, action]]
        // $router->addRoute("GET", "/api/users", $userController, "listAction");

        public function addRoute($method, $url, $controller, $action)
        {
                if (!isset($this->routes)) {
                        $this->routes = array();
                }
                if (!isset($this->routes[$url])) {
                        $this->routes[$url] = array();
                }
                $route_item = &$this->routes[$url];
                $route_item[$method] = array(
                        "controller" => $controller,
                        "action" => $action
                );
                unset($route_item);
        }

        public function displayAll()
        {
                var_dump($this->routes);
        }
        public function match($method, $url)
        {
                if (!isset($this->routes[$url])) {
                        // 匹配正则路由
                        return $this->antPattern(url: $url, method: $method);
                }
                if (!isset($this->routes[$url][$method])) {
                        return false;
                }
                $route_item = $this->routes[$url][$method];
                $controller = $route_item["controller"];
                $action = $route_item["action"];

                if (method_exists($controller, $action)) {
                        return array(
                                "controller" => $controller,
                                "action" => $action
                        );
                } else {
                        error_log("方法不存在: " . get_class($controller) . " 中未找到 {$action}() 方法");
                        return false;
                }
        }
        public function antPattern($url, $method)
        {
                foreach ($this->routes as $route_url => $route_item) {
                        // 判断$route_url 是否包含regex:, 如果不包含跳过
                        if (strpos($route_url, "regex:") === false) {
                                continue;
                        }
                        $route_url = str_replace("regex:", "", $route_url);
                        error_log("匹配路由: " . $route_url . ", url:" . $url . "");
                        if (preg_match($route_url, $url, $matches)) {
                                if (isset($route_item[$method])) {
                                        $route_item = $route_item[$method];
                                        $controller = $route_item["controller"];
                                        $action = $route_item["action"];
                                        if (method_exists($controller, $action)) {
                                                return array(
                                                        "controller" => $controller,
                                                        "action" => $action,
                                                        "params" => $matches
                                                );
                                        } else {
                                                error_log("方法不存在: " . get_class($controller) . " 中未找到 {$action}() 方法");
                                                return false;
                                        }
                                }
                        }
                }
        }
        public function getRoutes()
        {
                return $this->routes;
        }
}

?>